import { ref, Ref } from 'vue';
import type { BehaviorCaptchaCategory, CaptchaResource, CaptchaData } from '/@/lib/declarations';

import { useOpenApi } from '/@/apis';
import { useCryptoStore } from '/@/stores';

export default function useBehaviorCaptcha() {
	const timer = ref<ReturnType<typeof setTimeout> | null>(null);
	const message = ref<string>('');
	const canOperate = ref<boolean>(true);
	const isLoading = ref<boolean>(false);
	const isSuccess = ref<boolean>(false);
	const isShowMessage = ref<boolean>(false);
	const schema = ref({}) as Ref<CaptchaResource>;

	const successText = '验证通过!';
	const failedText = '验证失败，请重试!';
	const defautImage =
		'iVBORw0KGgoAAAANSUhEUgAAAQQAAACgCAIAAABlkIURAAAFQ0lEQVR42u3dsXLbMAyA4b7/e2XNmLFj1o4ZXV1wh+JAiZZdt5bt7x9yiRPRiYPfBEiR/PEF4JsfXgKADAAZADIAZADIAJABIANABoAMABkAMgBkAMgAkAEgA0AGgAwAGQAyAGQAyACQASADQAaADAAZADIAZADIAJABIANABoAMABkAMgBkAEAGgAwAGQAyAGQAyACQASADQAaADAAZADIAZDgcP7fx4pDhtfj4+HhfY3nci0MGMpCBDC/Dr8JEhl//C/8RMtyHz8/P94OhPiHDsfKiOyIlIwMZyECGY8iw5EtLvk4GMpDhfTHhdDodoaMgAxnuKcPSLYxZ03wEaSlzyUCGJ5Th9E2TYQn30zZ7cqqltZi9XmRbPu7sdshAhgeTYT4mu1y7NWNwdjCXDGR4kp4hkqsI+tYVREcRzzvJsshAhsPJsHz+uc1qNEccT2ayc+Rq0reQgQyHk+G6wrf1GB+FlkctzzgxCmR4YBlilKl1LEub2XgbgwofxmckAxkeW4YW2VEb1BpjtXpu/pCBDM8gwxLTGegRzdlmraTHknp8UjKQ4YFlaDEd9XGMOJ1tcBxZIgMZHliGSIri2uwW9pgQ5rTciQxkeHgZJmNK82vJQIanlWH/nRc5V00GMjyhDBfx9vY23jpOBjI8TwF99TAUGcjw8DKsDgrtt8hoEhmeSoaYbL7JVWQgwyFk+Msbky7qHLYuIQMZHluGyb1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i5yAAAAAElFTkSuQmCC';

	const openApi = useOpenApi();
	const crypto = useCryptoStore();

	const verifyCaptcha = (category: BehaviorCaptchaCategory, data: CaptchaData, successCallback: () => void, failedCallback: () => void) => {
		canOperate.value = false;
		isLoading.value = true;

		const identity = crypto.sessionId;

		openApi
			.verifyCaptcha(identity, category, data)
			.then((result) => {
				if (result.data) {
					message.value = successText;
					isSuccess.value = true;
					isShowMessage.value = true;
					canOperate.value = false;
					timeoutSet(successCallback);
				}
			})
			.catch((error) => {
				if (error.code && error.code === 40610) {
					// 失败
					message.value = failedText;
				} else {
					message.value = error.message;
				}
				isSuccess.value = false;
				isShowMessage.value = true;
				canOperate.value = false;
				timeoutSet(failedCallback);
			});
	};

	const fetchCaptcha = (category: BehaviorCaptchaCategory) => {
		isLoading.value = true;

		const identity = crypto.sessionId;

		if (identity) {
			openApi
				.createCaptcha(identity, category)
				.then((result) => {
					schema.value = result.data as CaptchaResource;
					canOperate.value = true;
					isLoading.value = false;
				})
				.catch(() => {
					isLoading.value = false;
				});
		}
	};

	const reset = () => {
		message.value = '';
		isSuccess.value = false;
		isShowMessage.value = false;
		canOperate.value = true;
		isLoading.value = false;
	};

	const getImage = (date: string): string => {
		let image = '';
		if (date) {
			image = date;
		} else {
			image = defautImage;
		}

		const prefix = 'data:image/png;base64,';
		if (image.startsWith(prefix)) {
			return image;
		} else {
			return prefix + defautImage;
		}
	};

	const timeoutClear = () => {
		if (timer.value) {
			clearTimeout(timer.value);
		}
	};

	const timeoutSet = (callback: () => void) => {
		timer.value = setTimeout(callback, 800);
	};

	return {
		getImage,
		timeoutClear,
		timeoutSet,
		message,
		canOperate,
		isLoading,
		isSuccess,
		isShowMessage,
		verifyCaptcha,
		reset,
		fetchCaptcha,
		schema,
	};
}
